<?php ini_set("display_errors","2");ERROR_REPORTING(E_ALL);
/*
	ProcessCommunityTotals.php
	
	NOTE:  I expect $CT_id to be an INT var passed via the POST array.
	I know it will be an INT because it is a cast var from the 
	community totals form.
	
*/
include('./../includes/phpheader.php');
include('./../includes/db_connect.php'); 
	//community total id (is there is one)
   $CT_id = $_POST["CT_id"];
	
  //print_r($CT_id);
  //die();
  
  //Coordinator Name
  $CT_coordinator = $_POST["serviceRecordCoordinator"];
  //Coordinator Name
  $CT_coordinatorID = $loggedInUserID;

  //CT Date
  $CT_month = $_POST["serviceRecordMonth"];
  $CT_year = $_POST["serviceRecordYear"];
  
  //Number of Contacts
  $CT_contacts = $_POST["totalServiceRecordContacts"];	

  //  Service Record Hour Totals
  $CT_serviceHoursTotal = $_POST["serviceRecordHoursTotal"];
  
  
  //query db to see what we have so far based a community total ID passed to us  
  $query = "SELECT CT_id 
			 FROM community_totals 
			 WHERE CT_id = ".$CT_id;
   
  
  
  if(mysql_fetch_array(mysql_query($query))) {

	//echo "<h1>".$CT_id." in community_totals update.</h1>";
	
	$CT_update_query = "UPDATE `community_totals` 
							SET `CT_id` = ".$CT_id.", 
								 `CT_Coordinator_ID` = '".$CT_coordinatorID."', 
								 `CT_Coordinator_Name` = '".$CT_coordinator."',"." 
								 `CT_Month` = '".$CT_month."',	
								 `CT_Year` = '".$CT_year."', 
								 `CT_Contacts`  = '".$CT_contacts."',
								 `CT_Service_Hours_Total`  = '".$CT_serviceHoursTotal."'
							WHERE CT_id = '".$CT_id."';";
		
		//perform the query
		$community_totals_update_result = mysql_query($CT_update_query);
		
		// Check result
		// This shows the actual query sent to MySQL, and the error. Useful for debugging.
		if (!$community_totals_update_result) {
			$message  = '<b>Invalid query:</b><br>'. mysql_error().'<br><br>';
			$message .= '<b>Whole query:</b><br>'.$query.'<br><br>';
			die($message);
		}
	
	} else {
	
		//echo "<h1>".$CT_id." in community_totals insert.</h1>";
		//setup insert query
	
		$CT_insert_query = "INSERT INTO community_totals (`CT_id` ,
																`CT_Coordinator_ID`, 
													           `CT_Coordinator_Name` , 
													           `CT_Month` , 
													           `CT_Year` , 
													           `CT_Contacts` ,
													           `CT_Service_Hours_Total`)
							  VALUES (NULL,
									 '".$CT_coordinatorID."', 
						           '".$CT_coordinator."', 
						           '".$CT_month."', 
						           '".$CT_year."',
						           '".$CT_contacts."',
						           '".$CT_serviceHoursTotal."')";
	
			//perform query
			$community_totals_insert_result = mysql_query($CT_insert_query);
  
			// Check result
			// This shows the actual query sent to MySQL, and the error. Useful for debugging.
			if (!$community_totals_insert_result) {
				$message  = '<b>Invalid query:</b><br>' . mysql_error() . '<br><br>';
				$message .= '<b>Whole query:</b><br>' . $query . '<br><br>';
				die($message);
				
			}else{
				
				$CT_id = mysql_insert_id();
				//echo "<h1>".$CT_id."</h1>";
				//die();
			}
	}


if(mysql_query($query)) {


	//we need to get the CT_ID from our community totals table so we can use it
	//as a foreign key in the community_totals_service table
	$CT_Idquery = "SELECT CT_id from community_totals where CT_id = ".$CT_id;
	$CT_Idrow = mysql_fetch_array(mysql_query($CT_Idquery));
		
	$retrieved_CTID = $CT_Idrow["CT_id"];
		
	//echo "<br /><h1>Retrieved_CTID: ".$retrieved_CTID."</h1>";
	//die();
	
	//this query gets IDs of all possible community totals categories
	$CT_CategoryQuery = "SELECT * FROM lst_community_totals";
	$CT_CategoryResult = mysql_query($CT_CategoryQuery);
	
	
	while($categoryRow = mysql_fetch_array($CT_CategoryResult)) {
	
			$categoryId          = $categoryRow["id"];
			$categoryServiceType = $categoryRow["community_total_type"];
			
			//nice way to print POST ARRAY
			//print("<pre>".print_r($_POST,true)."</pre>");

			
			//this accounts for the form elements entered by service category
			//and passed with POST array
			$category 							 	= $_POST[$categoryId."_category"];
			$categoryHours 					    = $_POST[$categoryId."_hours"];
			$categoryServiceDescription 	    	= $_POST[$categoryId."_service"];			
		
			if($category) {
	
				$CT_service_query = "SELECT id 
									    FROM community_totals_service 
									    WHERE stored_CT_id = '".$retrieved_CTID."' 
									    AND CT_Service_id = '".$categoryId."'";
										 
										 
				//echo "<h1>CT_service_query: ".$CT_service_query."</h1>";
			   //die();
			 
				$CT_service_query_result = mysql_query($CT_service_query);
				
				//$row = mysql_fetch_array($CT_service_query_result);
				//echo $row['id'];
				
				if($CT_service_row = mysql_fetch_array($CT_service_query_result)) {
				
					//echo print_r($CT_service_row);
					//die();
		
					//echo $categoryHours;
					//echo "<br />";
					//echo $categoryServiceDescription;
					//echo "<br />";
					//echo $CT_service_row['id'];
					//die();
		
					$CT_service_update = "UPDATE community_totals_service 
											 SET `CT_Service_Hours`  = '".$categoryHours."', 
											     `CT_Service_Description` = '".$categoryServiceDescription."' 
											 WHERE id =".$CT_service_row["id"];
				
				
				
					//perform the query
					$community_totals_service_update_result = mysql_query($CT_service_update);
					
					
					//if(!mysql_query($CT_service_update)) {
					// Check result
					// This shows the actual query sent to MySQL, and the error. Useful for debugging.
					if (!$community_totals_service_update_result) {
						$message  = '<b>Invalid query:</b><br>' . mysql_error() . '<br><br>';
						$message .= '<b>Whole query:</b><br>' . $CT_service_update. '<br><br>';
						die($message);
					}	
					
					
					
				
				} else {
					
					$CT_service_insert = "INSERT into community_totals_service (id,
																					  stored_CT_id,
																					  CT_Service_id,
																					  CT_Service_Type,
																					  CT_Service_Hours,
																					  CT_Service_Description) 
											VALUES (NULL,
													 '".$retrieved_CTID."', 
													 '".$categoryId."', 
													 '".$categoryServiceType."', 
												     '".$categoryHours."',
													 '".$categoryServiceDescription."')";
				
					/*
					if(!mysql_query($CT_service_insert)) {
						echo "<br/>Insert Error in community_totals_service table<br/>".$CT_service_insert;
					} 
					*/
					
					//perform the query
					$community_totals_service_insert_result = mysql_query($CT_service_insert);
					
					// Check result
					// This shows the actual query sent to MySQL, and the error. Useful for debugging.
					if (!$community_totals_service_insert_result) {
						$message  = '<b>Invalid query:</b><br>' . mysql_error() . '<br><br>';
						$message .= '<b>Whole query:</b><br>' . $CT_service_insert. '<br><br>';
						die($message);
					}
					
				}
		}
	}

  
	//header("Location:../admin.php?action=cieedit&id=".$id."&Status=Monthly Individual Service Record Form Submitted Successfully");
	//need to send this to a better page location
	header("Location:../admin.php?action=community_totals_list&Status=Your Community Totals Record was successfully handled by the database");
	exit();
   //echo "Success <br/>".$query;
   
   
} else {

	//There was a problem with the query, show an error message
	header("Location:../admin.php?action=community_totals_list&Status=ERROR. Your Community Totals Record was not handled properly.");
	die('Could not connect: ' . mysql_error());
	//header("Location:../admin.php?action=cieedit&id=".$id."&Error=Unable to process Monthly Individual Service Record Form due to system error.  Please contact system administrator.");
	//exit();
	echo "Failure <br/>".$query;
}
?>